import scrapy
import jsonpath
from LolScrapyRedis.items import LolscrapyredisItem
from scrapy_redis.spiders import RedisSpider

from loguru import logger


class LolscarpyredisSpider(RedisSpider):
    name = 'lolScarpyRedis'
    # allowed_domains = ['game.qtimq.cn']
    # start_urls = ['https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2749542']
    redis_key = 'awa'


    def __init__(self,*args,**kwargs):
        # Dynamically define the allowed domains list
        domain = kwargs.pop('domain','')
        self.allowed_domains = filter(None,domain.split(','))
        print(self.allowed_domains)

        super(LolscarpyredisSpider,self).__init__(*args,**kwargs)

    def make_requests_from_url(self, url):
        yield scrapy.Request(url=url)

    def parse(self, response):
        json_data = response.json()
        hreo_id = jsonpath.jsonpath(json_data, '$..heroId')[:50]
        for heroid in hreo_id:
            join_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js?ts=2749545'.format(heroid)
            yield scrapy.Request(
                url=join_url,
                callback=self.get_queue_url_parse_data,
                dont_filter=True,
            )

    def get_queue_url_parse_data(self,response):

        json_data = response.json()
        skins_list = jsonpath.jsonpath(json_data, '$..skins')[0]
        # logger.info(skins_list)
        heroName = jsonpath.jsonpath(json_data, '$..skins')[0][0]['heroName']


        for skins in skins_list:
            # 皮肤名称
            skins_name = ''.join(skins['name']).replace('/', '')
            "电脑版壁纸"
            win_mianImg = ''.join(skins['mainImg'])
            "手机版壁纸"
            phone_loadingImg = ''.join(skins['loadingImg'])
            if (win_mianImg == '') or (phone_loadingImg == ''):
                continue
            logger.info(skins_name)
            logger.info("电脑壁纸:{}".format(win_mianImg))
            logger.info("手机壁纸:{}".format(phone_loadingImg))

            # # self.process_run(win_mianImg, phone_loadingImg, os_path, skins_name)
            if 'big' in win_mianImg:
                version = 'PC'
                item = LolscrapyredisItem()
                item['skins_name'] = skins_name
                item['heroName'] = heroName
                item['version'] = version
                list_item = []
                list_item.append(win_mianImg)
                item['win_mainImg'] = list_item
                logger.debug(item)
                yield item
                logger.success('{}电脑版保存完毕'.format(skins_name))

            if 'loading' in phone_loadingImg:
                version = 'APP'
                item = LolscrapyredisItem()
                item['skins_name'] = skins_name
                item['heroName'] = heroName
                item['version'] = version
                list_item = []
                list_item.append(phone_loadingImg)
                item['phone_loadingImg'] = list_item
                logger.debug(item)
                yield item
                logger.success('{}手机版保存完毕'.format(skins_name))



